package com.wudl.flink.hbase.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author ：wudl
 * @date ：Created in 2021-12-09 0:38
 * @description：
 * @modified By：
 * @version: 1.0
 */

public class PropertyUtil {

    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    private static Properties props;
    static{
        loadProps();
    }

    synchronized static private void loadProps(){
        String propertiesName = "Application.properties";
        props = new Properties();
        InputStream in = null;
        try {
            //        <!--通过类加载器进行获取properties文件流-->
            in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertiesName);
            props.load(in);
        } catch (FileNotFoundException e) {
            logger.error(propertiesName + "common.properties文件未找到");
        } catch (IOException e) {
            logger.error("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error(propertiesName + "文件流关闭出现异常");
            }
        }
    }

    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }

    public static void main(String[] args) {
        String k1 = getProperty("k1");
        System.out.println(k1);

    }


}
